/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       combat_context_army.h

	$Header: /game/combat_context_army.h $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"
#pragma warning( disable: 4786 ) // Disable the ever so helpful "identifier was truncated to '255' characters" warning

#include "combat_context_army.h"

#include "adventure_map.h"
#include "adventure_map_window.h"
#include "adventure_frame.h"
#include "army.h"
#include "army_mover.h"
#include "combat_result.h"

t_combat_context_army::t_combat_context_army()
{
	m_from_saved_game = false;
}

t_combat_context_army::t_combat_context_army( t_army*	attacker, t_army*	defender,
											  t_adv_map_point const& original_defender_position )
					 : t_combat_context_adv_object( attacker, defender, defender->get_position() )
{
	m_defending_army = defender;
	m_from_saved_game = false;
	m_original_defender_position = original_defender_position;
}

t_combat_context_type t_combat_context_army::get_type() const
{
	return k_combat_context_type_army;
}

void t_combat_context_army::on_combat_end( t_combat_result result )
{
	t_combat_context_adv_object::on_combat_end( result );

	if (m_from_saved_game && defender_won( result )   
		&& m_defending_army->get_position() != m_original_defender_position)
	{
		t_adventure_map*		map = m_defender->get_map();
		t_adventure_map_window* map_window = map->get_map_window();

		if (map_window->get_object_mover() == 0)
			map_window->move_enemy_army( m_defending_army, m_original_defender_position );
	}
}

static int const k_current_version = 0;

bool t_combat_context_army::read( std::streambuf&  stream, t_adventure_map& adventure_map )
{
	int version = get<t_uint16>( stream );

	if (version < 0 || version > k_current_version)
		return false;
	if (!t_combat_context_adv_object::read( stream, adventure_map ))
		return false;
	::read( stream, &m_original_defender_position, sizeof( m_original_defender_position ));
	m_defending_army = dynamic_cast<t_army*>( m_defender );
	m_from_saved_game = true;
	return true;
}

bool t_combat_context_army::write( std::streambuf& stream ) const
{
	put<t_uint16>( stream, k_current_version );
	if (!t_combat_context_adv_object::write( stream ))
		return false;
	::write( stream, &m_original_defender_position, sizeof( m_original_defender_position ));
	return true;
}

